package com.zdsoft.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zdsoft.service.IArticleService;

@Controller
@RequestMapping("/")
public class IndexController {
	@Autowired
	IArticleService aService;
	@GetMapping
	public ModelAndView index(
			@RequestParam(name="page",defaultValue="1") Integer pageNum,
	@RequestParam(name="limit",defaultValue="4")Integer pageSize,
	@RequestParam(name="title",defaultValue="")String title){
		if(pageNum <0){
			pageNum=0;
		}
		long pageCount=aService.getTotal(title)/pageSize;
		if(pageNum >aService.getTotal(title)/pageSize){
			pageNum=(int)pageCount;
		}
		ModelAndView mav=new ModelAndView("blog");
		Map<String,Object> map =new HashMap<>();
		mav.addObject("title", title);
		mav.addObject("page",pageNum);
		mav.addObject("count",aService.getTotal(title));
		mav.addObject("limit",pageSize);
		mav.addObject("articleList", aService.getArticleList(pageNum,pageSize, title));
		
		return mav;
	}
}
